import { request } from '@umijs/max'

interface ApiResponse<T> {
  code: number
  msg: string
  data: any
  success: boolean
}

// 联系人类型
export interface ContactItem {
  address: string;
  aiAssistantStatus: number;
  blockType: number;
  canChat: boolean;
  domain: string;
  friendAccid: string;
  friendCode: string;
  friendOnlineStatus: string;
  lastMessage: string;
  lastMessageRevoked: number;
  lastMessageTime: number;
  lastMessageTypes: number[];
  nickName: string;
  pic: string;
  privateChatId: number;
  readState: number;
  unreadMsgNum: number;
  userCode: string;
  userName: string;
  totalRealRevenueUsd: number;
  nicknameNote: string;
  nicknameRemark: string;
}

// 聊天消息媒体类型
interface ChatMediaItem {
  blurPic: string;
  cover: string;
  duration: number;
  height: number;
  isCharge: boolean;
  isFromVault: number;
  type: number;
  url: string;
  width: number;
}

// 聊天消息详情
interface ChatMessageInfo {
  agoraMsgId: string;
  beFavorite: number;
  classification: number;
  createTime: number;
  firstMsgFlag: number;
  freeMediaList: ChatMediaItem[];
  id: number;
  isCharge: number;
  isPaid: number;
  lastMsgFlag: number;
  locked: number;
  msgContent: string;
  messageContent: string;
  msgTypes: string[];
  paidMediaList: ChatMediaItem[];
  price: number;
  privateChatId: number;
  readState: number;
  receiverUser: string;
  relateMediaId: number;
  replyContent: string;
  replyMessageId: number;
  replyMessageTime: number;
  replyMsgId: number;
  revoked: number;
  senderUser: string;
}

// 按日期分组的消息列表
export interface ChatMessageGroup {
  messageDate: string;
  messageInfoList: ChatMessageInfo[];
}

// 聊天消息列表请求参数
interface ChatMessageListParams {
  current: number;
  endTime: number;
  privateChatId: number;
  size: number;
  startTime: number;
  targetUserCode: string;
  timezone: string;
}

/** 获取用户code */
export async function domain2userCode(data: any) {
  return request<ApiResponse<any>>('/api/backdoor/transfer/domain2userCode', {
    method: 'POST',
    data
  })
}

/** 获取 chat token */
export async function queryToken(data: any) {
  return request<ApiResponse<any>>('/api/ofm/ofm-chat/v1/queryToken', {
    method: 'POST',
    data
  })
}

/** ofm获取聊天联系人列表 */
export async function queryChatPairList({
  current,
  size,
  targetUserCode,
}: {
  current: number;
  size: number;
  targetUserCode: string;
}) {
  return request<ApiResponse<any>>('/api/ofm/ofm-chat/v1/queryChatPairList', {
    method: 'POST',
    data: {
      current,
      size,
      targetUserCode,
    }
  })
}

/** ofm设置某个会话消息已读 ofm/ofm-chat/setChatRead */
export async function setChatRead({
  privateChatId,
  targetUserCode,
}: {
  privateChatId: number;
  targetUserCode: string;
}) {
  return request<ApiResponse<any>>('/api/ofm/ofm-chat/setChatRead', {
    method: 'POST',
    data: {
      privateChatId,
      targetUserCode,
    }
  })
}

/** ofm上报聊天消息(免费消息) */
export async function reportChatMsg(data: any) {
  return request<ApiResponse<any>>('/api/ofm/ofm-chat/reportChatMsg', {
    method: 'POST',
    data
  })
}

/** ofm上报聊天消息(付费消息) */
export async function reportChatMsgPaid(data: any) {
  return request<ApiResponse<any>>('/api/ofm/ofm-chat/sendPaidMessage', {
    method: 'POST',
    data
  })
}


/** ofm根据privateChatId获取聊天消息列表 ofm/ofm-chat/queryChatMsgList */
export async function queryChatMsgList(params: ChatMessageListParams) {
  return request<ApiResponse<{ results: ChatMessageGroup[] }>>('/api/ofm/ofm-chat/queryChatMsgList', {
    method: 'POST',
    data: params
  });
}


export interface Material {
  blurURL: string;
  coverUrl: string;
  createTime: number;
  favorite: number;
  height: number;
  id: number;
  mediaType: number;
  source: number;
  sourceId: number;
  url: string;
  width: number;
}

/* OFM获取素材列表 ofm/ofm-chat/find-material **/
export async function findMaterial({
  categoryId,
  current,
  mediaType,
  targetUserCode,
}: {
  categoryId: number;
  current: number;
  mediaType?: 0 | 1 | 2; // 0 文字 1 图片 2 视频
  targetUserCode: string;
}) {
  return request<ApiResponse<Material>>('/api/ofm/ofm-chat/find-material', {
    method: 'POST',
    data: {
      categoryId,
      current,
      mediaType,
      size: 20,
      targetUserCode,
    }
  })
}

/** ofm获取分类列表 ofm/ofm-chat/find-category */
export async function findCategory({
  targetUserCode,
}: {
  targetUserCode: string;
}) {
  return request<ApiResponse<any>>('/api/ofm/ofm-chat/find-category', {
    method: 'POST',
    data: {
      targetUserCode,
    }
  })
}


/** ofm重命名联系人 ofm/ofm-user/v1/add-nickname-note **/
export async function addNicknameNote(data: any) {
  return request<ApiResponse<any>>('/api/ofm/ofm-user/v1/add-nickname-note', {
    method: 'POST',
    data
  })
}


// 查询最近联系用户列表：用于网易云信订阅
export async function queryRecentContactedUsers(data: any) {
  return request<ApiResponse<any>>('/api/ofm/ofm-chat/queryFriendUserIdList', {
    method: 'POST',
    data
  })
}
